﻿using System;
using System.ComponentModel;
using System.Linq;
using FluentValidation;
using JHECIS.DataModel.Interface;
using Prism.Mvvm;

namespace JHECIS.DataModel
{
    public class InterfaceValidation : AbstractValidator<InterfaceToolEntity>
    {
        public InterfaceValidation()
        {
            RuleFor(a => a.CLASSNAME).NotEmpty().WithMessage("接口分类不能为空");
            RuleFor(a => a.INTERFACECODE).NotEmpty().WithMessage("接口编码不能为空");
            RuleFor(a => a.INTERFACENAME).NotEmpty().WithMessage("接口名称不能为空");
            RuleFor(a => a.FUNCTIONNAME).NotEmpty().WithMessage("方法名不能为空");
        }
    }
    public class ScriptContentValidation : AbstractValidator<ScriptContent>
    {
        public ScriptContentValidation()
        {
            RuleFor(a => a.JavaScriptCode).NotEmpty().WithMessage("JavaScript代码不能为空");
            RuleFor(a => a.FunctionName).NotEmpty().WithMessage("方法名不能为空");
            RuleFor(a => a.Data).NotEmpty().WithMessage("输入参数不能为空");
        }
    }
    public class ScriptContent : BindableBase, IDataErrorInfo
    {
        private string _data;
        public string Data
        {
            get => _data;
            set
            {
                _data = value;
                RaisePropertyChanged(nameof(Data));
            }
        }

        private string _javaScriptCode;
        public string JavaScriptCode
        {
            get => _javaScriptCode;
            set
            {
                _javaScriptCode = value;
                RaisePropertyChanged(nameof(JavaScriptCode));
            }
        }

        private string _functionName;
        private int scriptType;

        public string FunctionName
        {
            get => _functionName;
            set
            {
                _functionName = value;
                RaisePropertyChanged(nameof(FunctionName));
            }
        }

        private readonly ScriptContentValidation validator = new ScriptContentValidation();
        public string this[string columnName]
        {
            get
            {
                var firstOrDefault = validator.Validate(this).Errors
                    .FirstOrDefault(lol => lol.PropertyName == columnName);
                if (firstOrDefault != null)
                    return validator != null ? firstOrDefault.ErrorMessage : "";
                return string.Empty;
            }
        }

        /// <summary>
        /// 0:javascript 1:python
        /// </summary>
        public int ScriptType
        {
            get => scriptType;
            set
            {
                scriptType = value;
                RaisePropertyChanged(nameof(ScriptType));
            }
        }

        public string Error
        {
            get
            {
                var results = validator?.Validate(this);
                if (results != null && results.Errors.Any())
                {
                    var errors = string.Join(Environment.NewLine,
                        results.Errors.Select(x => x.ErrorMessage).ToArray());
                    return errors;
                }

                return string.Empty;
            }
        }
    }
}